package com.ruoyi.system.service;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @ProjectName: back
 * @Package: com.ruoyi.system.service
 * @ClassName: BaseService
 * @Author: haiyue
 * @Description:
 * @Date: 2025/5/14 上午 10:48:33
 * @Version: 1.0
 */
@Service
public class BaseService {

    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 判断该手机号是否注册
     * @param phone
     * @return true 未注册 false  已注册
     */
    public boolean checkPhone(String phone){
        SysUser sysUser = new SysUser();
        sysUser.setPhonenumber(phone);
        sysUser.setStatus("0");
        List<SysUser> userList = sysUserMapper.selectUserList(sysUser);
        return CollectionUtils.isEmpty(userList);
    }

    /**
     * @Author haiyue
     * @Description 当前天数增加天数
     * @Date 下午 1:57:38 2025/6/3
     * @param daysToAdd
     * @return java.util.Date
     **/
    public Date addDaysAndSetToEndOfDay(Long daysToAdd) {
        // 获取当前日期时间
        LocalDateTime now = LocalDateTime.now();

        // 增加指定天数
        LocalDateTime futureDate = now.plusDays(daysToAdd);

        // 设置时间为当天的23:59:59
        LocalDateTime endOfDay = futureDate.with(LocalTime.MAX);

        // 转换为Date对象（适用于Java 8+）
        Date date = Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());

        return DateUtil.endOfDay(date).offset(DateField.MILLISECOND, -999);
    }
    
    /**
     * 指定日期增加天数
     * @Date 下午 1:55:11 2025/6/3
     * @param oldDate
     * @param daysToAdd
     * @return java.util.Date
     **/
    public Date addDaysAndSetToEndOfDay(Date oldDate,Long daysToAdd) {
        // 1. 将 Date 转换为 Timestamp
        Timestamp timestamp = new Timestamp(oldDate.getTime());

        // 2. 将 Timestamp 转换为 LocalDateTime
        LocalDateTime localDateTime = timestamp.toLocalDateTime();

        // 增加指定天数
        LocalDateTime futureDate = localDateTime.plusDays(daysToAdd);

        // 设置时间为当天的23:59:59
        LocalDateTime endOfDay = futureDate.with(LocalTime.MAX);

        // 转换为Date对象（适用于Java 8+）
        Date date =  Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());

        return DateUtil.endOfDay(date).offset(DateField.MILLISECOND, -999);
    }

    /**
     * 生成电子签认证openId
     * @return
     */

    public String createCertificationOpenId(){
        String orderNumber = "C";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
        String time = sdf.format(new Date());
        orderNumber=orderNumber+time;
        //4位随机数
        Random random = new Random();
        int min = 1000;
        int max = 9999;
        int randomNum = random.nextInt(max - min + 1) + min;
        orderNumber = orderNumber+randomNum;
        return orderNumber;
    }
}
